package com.utils;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;

public class SearchNetease {
	final static String neteaseServerUrl = "http://music.163.com/api/search/pc";
	final static String neteasePlayUrl = "http://music.163.com/song/media/outer/url?id=";
	private static String name;
	private static String singer;
	private static String url;
	private static String pic;
	private static String songid;
	
	public static void search(String s) throws Exception{
		// 添加参数
		Map<String, String> params = new HashMap<String, String>();
		params.put("s", s);
		params.put("offset", "1");
		params.put("limit", "1");
		params.put("type", "1");
		
		/*调用get请求，第一次搜索，获取歌曲id*/
		String res = HttpClientService.sendGet(neteaseServerUrl +
				"?s=" + s + "&offset=1&limit=1&type=1").toString();
		System.out.println(res);
		res = res.substring(res.indexOf("{"));
		com.alibaba.fastjson.JSONObject result = JSON.parseObject(res);// 转JSON
		String resultToString = result.toString();
		System.out.println(resultToString);
		JsonObject songs = ReadJSONNetEase.parseJSON(resultToString);
		
		name = songs.get("name").getAsString();
		songid = songs.get("id").getAsString();
		url = neteasePlayUrl + songid;
		
		JsonObject album = RJSONNetEasepic.parseJSON(songs);
		
		pic = album.get("blurPicUrl").getAsString();
		
		JsonObject artists = RJSONNeteasesinger.parseJSON(album);
		singer = artists.get("name").getAsString();
		
	}

	public static String getName() {
		return name;
	}

	public static void setName(String name) {
		SearchNetease.name = name;
	}

	public static String getSinger() {
		return singer;
	}

	public static void setSinger(String singer) {
		SearchNetease.singer = singer;
	}

	public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		SearchNetease.url = url;
	}

	public static String getPic() {
		return pic;
	}

	public static void setPic(String pic) {
		SearchNetease.pic = pic;
	}

	public static String getSongid() {
		return songid;
	}

	public static void setSongid(String songid) {
		SearchNetease.songid = songid;
	}

	public static String getNeteaseserverurl() {
		return neteaseServerUrl;
	}

	public static String getNeteaseplayurl() {
		return neteasePlayUrl;
	}
	
	

}
